package com.eschool.beans;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.eschool.model.SaasAdmin;
import com.eschool.model.User;
import com.eschool.service.UserService;
import com.eschool.service.impl.UserServiceImpl;

@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean {

	SaasAdmin admin = new SaasAdmin();
	User user = new User();
	UserService userService = new UserServiceImpl();

	public SaasAdmin getAdmin() {
		return admin;
	}

	public void setAdmin(SaasAdmin admin) {
		this.admin = admin;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String action() {
		if (userService.authenticateUser(user))
			return "CustomerHome";
		
		String message = "Email or Password is not valid";
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Message", message));
		return "login";
	}
	
	public String authenticateAdmin() {
		if (userService.authenticateSaasAdmin(admin))
			return "home";
		
		String message = "Email or Password is not valid";
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Message", message));
		return "admin_login";
	}

	public String send() {
		return "home";
	}
	
	public String logOut() {
		String pageName = userService.logOutUser();
		return pageName;
	}
}
